﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.Util;
using Lucene.Net.Search;
using Lucene.Net.Documents;

namespace ZLCMS.Web.Example.Lucene
{
    public partial class Search : System.Web.UI.Page
    {
        public class TempObject
        {
            public string Name { get; set; }

            public string Code { get; set; }

            public string PY { get; set; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            IList<TempObject> list = new List<TempObject>();

            LuceneUtil.Instance.IndexDirectory = Server.MapPath("~/index");
            Query query = LuceneUtil.Instance.CreateQuery(new string[] { "name", "py"}, TextBox1.Text);
            Hits hits = LuceneUtil.Instance.Search(query);
            for (int i = 0; i < hits.Length(); i++ )
            {
                Document doc = hits.Doc(i);
                IDictionary<string, string> data = LuceneUtil.Instance.GetHighLight(TextBox1.Text, doc, "<font color=\"red\">", "</font>");
                list.Add(new TempObject() { Name = data["name"], Code = data["code"], PY = data["py"] });
                //list.Add(new TempObject() { Name = doc.Get("name"), Code = doc.Get("code") });
            }
            Label1.Text = hits.Length().ToString();
            //ScriptManager.RegisterClientScriptBlock(this, typeof(WriteIndex), "script", "alert('" + hits.Length() + "')", true);
            repeater.DataSource = list;
            repeater.DataBind();
        }
    }
}
